import * as THREE from 'three';
// 真实地球
export default class {
  config: any;
  mesh: THREE.Group;
  earth: THREE.Mesh;
  material: THREE.MeshPhysicalMaterial;
  textureLoader = new THREE.TextureLoader();
  constructor(config) {
    this.config = config;
    this.createMaterial();
    this.createMesh();
  }
  createMaterial() {
    const options = {};
    const mapKeys = ['map', 'normalMap', 'displacementMap', 'metalnessMap', 'emissiveMap'];
    const colorKeys = ['color', 'emissive'];
    const vectorKeys = ['normalScale'];
    for (const key in this.config) {
      if (mapKeys.includes(key)) {
        options[key] = this.textureLoader.load(this.config[key]);
      }
      if (colorKeys.includes(key)) {
        options[key] = new THREE.Color(this.config[key]);
      }
      if (vectorKeys.includes[key]) {
        options[key] = new THREE.Vector2(this.config[key], this.config[key]);
      }
    }
    this.material = new THREE.MeshPhysicalMaterial(options);
  }
  createMesh() {
    const sphere = new THREE.SphereGeometry(1e3, 128, 128);
    this.earth = new THREE.Mesh(sphere, this.material);
    this.mesh.add(this.earth);
  }
  updateComponent(config) {
    this.config = config;
    this.material.dispose();
    this.createMaterial();
    this.earth.material = this.material;
  }
  destroy() {
    this.earth.geometry.dispose();
    this.material.dispose();
    this.mesh.remove(this.earth);
  }
}
